package sort;

import java.util.Arrays;

public class Sort {

    //插入排序
    public void insertSort(int[] arr) {
        for(int i = 1; i < arr.length; i++) {
            int key = arr[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if(arr[j] > key) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = key;
        }
    }

    //冒泡排序
    public void bubbleSort(int[] arr) {
        for(int i = 0; i < arr.length - 1; i++) {
            for(int j = 0; j < arr.length - i - 1; j++) {
                if(arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
    }

    //选择排序
    public void selectSort(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            int min = i;
            int j = i + 1;
            for(; j < arr.length; j++) {
                if(arr[min] > arr[j]) {
                    min = j;
                }
            }
            //交换
            swap(arr, min, i);
        }
    }

    //希尔排序
    public void shellSort(int[] arr) {
        int key = arr.length;
        while(key > 1) {
            key /= 2;
            shell(arr, key);
        }
        shell(arr, 1);
    }

    private void shell(int[] arr, int key) {
        for(int i = key; i < arr.length; i++) {
            int val = arr[i];
            int j = i - key;
            for(; j >= 0; j -= key) {
                if(arr[j] > val) {
                    arr[j + key] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + key] = val;
        }
    }


    private void swap(int[] arr, int j, int i) {
        int tmp = arr[j];
        arr[j] = arr[i];
        arr[i] = tmp;
    }

    //堆排序
    public void heapSort(int[] arr) {
        //先创建一个大根堆
        createBigHeap(arr);
        int end = arr.length - 1;
        while(end > 0) {
            swap(arr, 0, end);
            adjustDown(arr, 0, end);
            end--;
        }
    }

    private void createBigHeap(int[] arr) {
        int len = arr.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--) {
            //对每一颗子树向下调整
            adjustDown(arr, parent, len);
        }
    }

    private void adjustDown(int[] arr, int parent, int len) {
        int child = parent * 2 + 1;
        while(child < len) {
            if(child + 1 < len && arr[child] < arr[child + 1]) {
                child++;
            }
            if(arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }

}

class Main {
    public static void main(String[] args) {
        int[] arr = {4,1,5,6,3,7,8,2,9};
        Sort sort = new Sort();
        sort.heapSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
